<?php
session_start();
// this sets variables in the session 

include_once('connection.php');

     
	if(isset($_GET['id'])){
	
		
		
		$userID = $_SESSION['id'];
			
			
		
				
				
					 $arrlength=count($_SESSION['temp']);

						for($x=0;$x<$arrlength;$x++){
						
						$arr = explode("," ,$_SESSION['temp'][$x]);
						$productID = $arr[0];
						$productQuantity = $arr[1];
						
						$sql = "insert into `bought_product` (productID,userID,quantity,buy_date)
							VALUES ('$productID','$userID','$productQuantity','".date("Y/m/d")."') ";
						$result = mysql_query($sql);

			
							
						}
						
						$_SESSION['temp'] = null;
						$_SESSION['cart'] = null;
						
						//Send emails
						
	   echo '<script>
	          window.location.href = "../cart.php";
			   </script>';
	
	}
    else{
	if(!isset($_GET['accountNum'])){
	    
		$_SESSION['bellValue'] = $_POST['bellValue'];
		
	echo '<form action = "inc/buyCartProducts.php" method = "get">';
    echo 'Enter account number : <input type = "text" name = "accountNum" id = "accountNum">';
	echo '<input type = "submit" value = "Continue.."> ';
    echo '</form>';
	
    }else {
	
	$accountNum = $_GET['accountNum'];
	$sql = "SELECT * FROM bank WHERE accountNum = " . $accountNum;
    $result = mysql_query($sql);
     if ($row = mysql_fetch_array($result)) {
		
	  
	  if($row['balance'] >= $_SESSION['bellValue']){
		//$_SESSION['ok'] = 1;	
			 
			 
    $ID = $_SESSION['id'];
    $bellValue = $_SESSION['bellValue'];
	$sysDate = date("Y/m/d");
    $sql = "insert into bought_bell (userId,bellValue,date)
    				VALUES ('$ID','$bellValue' , '$sysDate') ";
    $result = mysql_query($sql);
	
   

    $sql = "UPDATE bank SET balance = balance - $bellValue WHERE accountNum=" . $accountNum;
	
    $result = mysql_query($sql);
	//$_SESSION['cart'] = null;
	
	/*echo '<script>$(".qouantity").each(function(){
            var currentProduct = $(this).attr("product");//product id
            var currentProductQuantity = $(this).val();
            $.ajax(
            {
                url:"inc/addHistory.php",
                data:"productID="+currentProduct+"&productQuantity="+currentProductQuantity,
                type: "post",
                success: function(j){
                
                }
            });
        });</script>';*/
	
		//$_SESSION['cart'] = null;
		
		  $sql = "UPDATE settings SET transactions = transactions + 1";
	
          $result = mysql_query($sql);
		
		echo " <script type='text/javascript'>  alert('Operation Done'); </script> ";
		
	   echo '<script>
	          window.location.href = "buyCartProducts.php?id=1";
			   </script>';
	  
	  }else {
	  $_SESSION['temp'] = null;
	   
	   echo " <script type='text/javascript'>  alert(' no enough charge '); </script> ";
	   echo '<script>
	          window.location.href = "../cart.php";
			   </script>';
	  
	  }
	  

    }
	
	else {
	       $_SESSION['temp'] = null;
	 echo " <script type='text/javascript'>  alert(' invalid account number '); </script> ";
		echo '<script>
	          window.location.href = "../cart.php";
			   </script>';
	}
	
	
	}
	
	
	
	


//$sql = "SELECT * FROM bank WHERE id = " . $_SESSION['id'];
//$result = mysql_query($sql);
/*if ($result) {
    $row = mysql_fetch_array($result);

}*/




/*..
    $sql = "SELECT * FROM `product`,`cart` WHERE cart.active = 1 AND cart.userId = " . $_SESSION['id'] . " AND cart.productId = product.id";
    $result = mysql_query($sql);
    $cartActive = mysql_num_rows($result);
    echo '<div id="cartProductsAreaTitle">
                    <img src="images/cartIcon.png"/>
                    <h2>Cart Items</h2>
                </div>';
//                    echo mysql_num_rows($result);
    while ($row = mysql_fetch_array($result)) {
        $price = $row['price'] - ($row['price'] * ($row['discount'] / 100));
        echo '<div content="' . $row['productId'] . '" class="cartProductsAreaSlices">
                                <div class="cartProductsAreaSlicesImage">
                                    <img src="images/products/' . $row['image'] . '" height="90%"/>
                                </div>
                                <div class="cartProductsAreaSlicesInfo">
                                    <div class="cartProductsAreaSlicesInfoTitle">
                                        <h3>' . $row['title'] . '</h3>
                                    </div>
                                    <div class="cartProductsAreaSlicesInfoDesc">
                                        <p> ' . $row['descreption'] . ' </p>
                                    </div>
                                    <div class="cartProductsAreaSlicesInfoPrice">
                                        <p class="price" content="' . $price . '">||Price: ' . $price . '</p>
                                        <p class="totalPrice">||Total Price: ' . $price . '</p>
                                        <p>||views: ' . $row['visited'] . '</p>
                                        <p>||Bought: ' . $row['buyCount'] . '</p>
                                        <div class="cartProductsAreaSlicesInfoPriceButtons">
                                            <a href="javascript:void(0)" class="deleteFromCart" content=' . $row['id'] . '><img src="images/delete.png" width="20px" alt="delete" /></a>
                                            <input value="1" type="text" price="' . $price . '" class="qouantity" product="' . $row['productId'] . '"/>
                                            <p>Quantity</p>
                                        </div>
                                    </div>

                                </div>
                            </div>';
    }
    if ($cartActive == 0) {
        echo '<div id="cartProductsAreaPerform">
                    <p style="display:block; margin:auto; width:140px; text-align:center; background-color:green;color:white; border-radius:5px;">Buying Approved</p>
                    </div>';
    } else {
        echo '<div id="cartProductsAreaPerform">
                    <a class="approveBuy" href="javascript:void(0)">Approve Buying</a>
                    <a class="discardBuy" href="javascript:void(0)">Discard Cart</a>
                    </div>';
    }


*/

//                echo $count;
//echo '<div id="cartProductsAreaTitle">
//        <img src="images/cartIcon.png"/>
//        <h2>Cart Items</h2>
//    </div>
//    
//    ';
}
?>

<html>
<head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="Hasan" content="EC" />
        <!--  CSS   -->
        <link href="../css/en/mycss.css" rel="Stylesheet" type="text/css" />
        <link href="../css/en/header.css" rel="Stylesheet" type="text/css" />
        <link href="../css/en/footer.css" rel="Stylesheet" type="text/css" />
        <link href="../css/en/cart.css" rel="Stylesheet" type="text/css" />
        <link rel="shortcut icon" href="../favicon.ico" />
        <!--  JS   -->
        <script src="../js/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script src="../js/myjs.js"></script>
        <script src="../js/cart.js"></script>
		<script src="../js/test.js"></script>
        <script src="../js/header.js"></script>

        <title>Mobizoo Mobiles</title>
		
    </head>
	
	
	

</html>